﻿@page "/files/edit/{Id:guid}"
@attribute [Authorize]

@using ClassifiedAds.Blazor.Modules.Files.Components

<div class="card">
	<div class="card-header">
		Edit File
	</div>
	<div class="card-body">
		<EditForm Model="File" OnValidSubmit="HandleValidSubmit">

			<DataAnnotationsValidator />
			<ValidationSummary />

			<div class="form-group row">
				<label for="fileName" class="col-sm-2 col-form-label">Name: </label>
				<div class="col-sm-10">
					<InputText id="fileName" class="form-control" @bind-Value="File.Name" placeholder="Enter Name"></InputText>
					<ValidationMessage For="@(() => File.Name)" />
				</div>
			</div>

			<div class="form-group row">
				<label for="description" class="col-sm-2 col-form-label">Description: </label>
				<div class="col-sm-10">
					<InputText id="description" class="form-control" @bind-Value="File.Description" placeholder="Enter Description"></InputText>
					<ValidationMessage For="@(() => File.Description)" />
				</div>
			</div>
			<div class="form-group row">
				<label for="formFile" class="col-sm-2 col-form-label">File</label>
				<div class="col-sm-10">
					<input id="formFile"
						   name="formFile"
						   class="form-control" value="@File.FileName" disabled />
				</div>
			</div>
			<div class="form-group row">
				<label for="encrypted" class="col-sm-2 col-form-label">Encrypted</label>
				<div class="col-sm-10">
					<InputCheckbox id="encrypted" @bind-Value="File.Encrypted" disabled />
				</div>
			</div>
			@if (File.FileEntryText != null)
			{
				<div class="form-group row">
					<label for="textLocation" class="col-sm-2 col-form-label">Text Location</label>
					<div class="col-sm-10">
						<a href="javascript:void(0)" @onclick="(() => DownloadText())">@File.FileEntryText?.TextLocation</a>
					</div>
				</div>
			}
			<div class="form-group row">
				<label for="description" class="col-sm-2 col-form-label"></label>
				<div class="col-sm-10">
					<button class="btn btn-primary">Save</button>
				</div>
			</div>
		</EditForm>
	</div>
	<div class="card-footer">
		<a class="btn btn-outline-secondary"
		   href="@("/files")"
		   style="width: 80px;">
			<i class="fa fa-chevron-left"></i> Back
		</a>
		&nbsp;
		<button @onclick="(ViewAuditLogs)" class="btn btn-secondary" title="View Audit Logs">View Audit Logs</button>
	</div>
</div>

<AuditLogsDialog @ref="AuditLogsDialog"></AuditLogsDialog>

@if (File.FileEntryEmbeddings != null)
{
	<div class="card">
		<div class="card-header">
			File Embedding
		</div>

		<div class="card-body">
			<div style="float: right;">
				<InputCheckbox id="showTokenDetails" @bind-Value="ShowTokenDetails" /> Show Token Details
			</div>
			<table class="table">
				<thead>
					<tr>
						<th>Chunk Name</th>
						<th>Short Text</th>
						<th>Created Date Time</th>
						<th>Embedding</th>
						@if (ShowTokenDetails)
						{
							<th>Token Details</th>
						}
					</tr>
				</thead>
				<tbody>
					@foreach (var fileEntryEmbedding in File.FileEntryEmbeddings)
					{
						<tr>
							<td title="@fileEntryEmbedding.ChunkLocation">
								<a href="javascript:void(0)" @onclick="(() => DownloadChunk(fileEntryEmbedding))">@fileEntryEmbedding.ChunkName</a>
							</td>
							<td>@fileEntryEmbedding.ShortText</td>
							<td>@fileEntryEmbedding.CreatedDateTime</td>
							<td><a href="javascript:void(0)" @onclick="(() => DownloadEmbedding(fileEntryEmbedding))">Download</a></td>
							@if (ShowTokenDetails)
							{
								<td>@fileEntryEmbedding.TokenDetails</td>
							}
						</tr>
					}
				</tbody>
			</table>
		</div>
	</div>
}